﻿/**
 * 格式化数字为中文单位（万、亿、万亿）
 * @example
 * formatMoneyCN(15000) → "1.5万"
 * formatMoneyCN(320000000) → "3.2亿"
 */
export function formatMoneyCN(num: number): string {
  if (num === null || num === undefined || isNaN(num)) {
    return "0";
  }

  const isNegative = num < 0;
  const absNum = Math.abs(num);
  let formatted: string;

  if (absNum < 10000) {
    formatted = Math.floor(absNum).toString();
  } else if (absNum < 100000000) {
    formatted = (absNum / 10000).toFixed(1);
    formatted = parseFloat(formatted).toString() + "万";
  } else if (absNum < 1000000000000) {
    formatted = (absNum / 100000000).toFixed(1);
    formatted = parseFloat(formatted).toString() + "亿";
  } else {
    formatted = (absNum / 1000000000000).toFixed(1);
    formatted = parseFloat(formatted).toString() + "万亿";
  }

  return isNegative ? "-" + formatted : formatted;
}
